Create a Spring Boot project with Kotlin | 您所在的位置:网站首页 › building web applications with spring boot and kotlin › Create a Spring Boot project with Kotlin |
Open the build.gradle.kts file: it is the Gradle Kotlin build script, which contains a list of the dependencies required for the application. The Gradle file is standard for Spring Boot, but it also contains necessary Kotlin dependencies, including the kotlin-spring Gradle plugin – kotlin("plugin.spring"). Here is the full script with the explanation of all parts and dependencies: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile // For `KotlinCompile` task below plugins { id("org.springframework.boot") version "2.7.1" id("io.spring.dependency-management") version "1.0.11.RELEASE" kotlin("jvm") version "1.8.20" // The version of Kotlin to use kotlin("plugin.spring") version "1.8.20" // The Kotlin Spring plugin } group = "com.example" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_17 repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jdbc") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") // Jackson extensions for Kotlin for working with JSON implementation("org.jetbrains.kotlin:kotlin-reflect") // Kotlin reflection library, required for working with Spring implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") // Kotlin standard library runtimeOnly("com.h2database:h2") testImplementation("org.springframework.boot:spring-boot-starter-test") } tasks.withType { // Settings for `KotlinCompile` tasks kotlinOptions { // Kotlin compiler options freeCompilerArgs = listOf("-Xjsr305=strict") // `-Xjsr305=strict` enables the strict mode for JSR-305 annotations jvmTarget = "17" // This option specifies the target version of the generated JVM bytecode } } tasks.withType { useJUnitPlatform() }As you can see, there are a few Kotlin-related artifacts added to the Gradle build file: In the plugins block, there are two Kotlin artifacts: kotlin("jvm") – the plugin defines the version of Kotlin to be used in the project kotlin("plugin.spring") – Kotlin Spring compiler plugin for adding the open modifier to Kotlin classes in order to make them compatible with Spring Framework features In the dependencies block, a few Kotlin-related modules listed: com.fasterxml.jackson.module:jackson-module-kotlin – the module adds support for serialization and deserialization of Kotlin classes and data classes org.jetbrains.kotlin:kotlin-reflect – Kotlin reflection library org.jetbrains.kotlin:kotlin-stdlib-jdk8 – Kotlin standard library After the dependencies section, you can see the KotlinComiple task configuration block. This is where you can add extra arguments to the compiler to enable or disable various language features. |
CopyRight 2018-2019 实验室设备网 版权所有 |